package ar.com.pragmatia.lanacion.data;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.database.Cursor;
import ar.com.pragmatia.lanacion.app.App;
import ar.com.pragmatia.lanacion.utils.Strings;

public class User extends AppData implements Datable {

	public static final String TABLE_NAME = "users";
	public static final String ID = "";
	public static final String FIELDS[] = { "id", "username", "password",
			"seller_code", "full_name", "device_id", "created", "modified",
			"last_login" };
	public static final String FIELDS2[] = { "id", "username", "seller_code",
			"full_name", "device_id" };

	private String username;
	private String password;
	private String sellerCode;
	private String fullName;
	private String deviceId;
	private String created;
	private String modified;
	private String lastLogin;

	public void initialize() {

		String[] fields = getFieldsName();

		String sql = "SELECT " + Strings.join(fields, ",") + " FROM "
				+ getTableName();

		Cursor c = App.getDataHelper().getDb().rawQuery(sql, null);

		if (c.getCount() == 1) {

			c.moveToFirst();

			for (int i = 0; i < fields.length; i++) {
				try {
					executeStringSetterMethod(this.getClass(),
							"set" + Strings.camelize(fields[i], false),
							c.getString(i));
				} catch (SecurityException e) {
					App.log("AppData", "save", e.getMessage());
				} catch (IllegalArgumentException e) {
					App.log("AppData", "save", e.getMessage());
				} catch (NoSuchMethodException e) {
					App.log("AppData", "save", e.getMessage());
				}
			}

		} else {

			setId("");
			setUsername("");
			setPassword("");
			setSellerCode("");
			setFullName("");
			setDeviceId("");
			setLastLogin("");
		}
		c.close();
	}

	public User() {
	}

	public User(JSONObject json) throws JSONException {
		if (json != null) {
			json = (JSONObject) json.getJSONObject("User");
			setUsername(json.getString("username"));
			setId(new Integer(json.getInt("id")).toString());
			setSellerCode(json.getString("seller_code"));
			setFullName(json.getString("full_name"));
			setDeviceId(json.getString("device_id"));
		}
	}

	public boolean isEmpty() {
		return Strings.isNullOrEmpty(getId());
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSellerCode() {
		return sellerCode;
	}

	public void setSellerCode(String sellerCode) {
		this.sellerCode = sellerCode;
	}

	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public String getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}

	public String getCreated() {
		return created;
	}

	public void setCreated(String created) {
		this.created = created;
	}

	public String getModified() {
		return modified;
	}

	public void setModified(String modified) {
		this.modified = modified;
	}

	public String getLastLogin() {
		return lastLogin;
	}

	public void setLastLogin(String lastLogin) {
		this.lastLogin = lastLogin;
	}

	public boolean save(boolean flag) {

		if (flag) {
			ContentValues values = new ContentValues();

			for (int i = 0; i < FIELDS2.length; i++) {

				String value = null;
				try {
					value = executeStringGetterMethod(this.getClass(), "get"
							+ Strings.camelize(FIELDS2[i], false));
				} catch (NoSuchMethodException e) {
					// e.printStackTrace();
					// App.log("AppData", "save", this.getClass().getName());
					App.log("AppData", "save", e.getMessage());
				}

				if (FIELDS2[i].equals("id") && Strings.isNullOrEmpty(value)) {
					continue;
				} else {
					value = (Strings.isNullOrEmpty(value)) ? "" : value;
					values.put(FIELDS2[i], value);
				}

			}

			long r = -1;
			try {
				r = App.getDb().insert(getTableName(), null, values);
			} catch (Exception e) {
				e.printStackTrace();
				App.log("AppData", "save", e.getMessage());
			}
			return (r >= 0) ? true : false;
		}
		return false;
	}

	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password
				+ ", sellerCode=" + sellerCode + ", fullName=" + fullName
				+ ", deviceId=" + deviceId + ", created=" + created
				+ ", modified=" + modified + ", lastLogin=" + lastLogin + "]";
	}

}
